package net.homelinux.chaoswg.io.renga.core.registries;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import net.homelinux.chaoswg.io.higherorderfunctions.ConstantFunction;
import net.homelinux.chaoswg.io.renga.core.gui.operations.Operation;

public class OperationRegistry {
    private static OperationRegistry _this;
    private static Map<String,ConstantFunction<? extends Operation>> operationMapping;
    
    private OperationRegistry () {
        operationMapping = new HashMap<String, ConstantFunction<? extends Operation>>();
    }
    
    public final static OperationRegistry getOperationRegistry () {
        if (_this == null) {
            _this = new OperationRegistry ();
        }
        return _this;
    }
    
    public void setOperationFactory (final String name, final ConstantFunction<? extends Operation> cf) {
        operationMapping.put(name, cf);
    }
    
    public Operation getOperation (final String name) {
        final ConstantFunction<? extends Operation> cf  = operationMapping.get(name);
        
        if (cf != null) {
            return cf.apply();
        }
        return null;
    }
    
    public Set<String> getOperationNames () {
        return new TreeSet<String> (operationMapping.keySet());
    }
}
